package model;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;

import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;
import org.farng.mp3.id3.FrameBodyAPIC;
import org.farng.mp3.id3.ID3v2_4;
import org.farng.mp3.id3.ID3v2_4Frame;

import persistence.Track;

import Exception.FileSystem.FileSystemException;
import View.MainWindow;

/**
 * 
 * FileSystem
 *
 * @author 
 * @sinde 30.11.2011
 *
 */
public class FileSystem {
	private String _actualPath;
	private Track[] _fileList;
	private String[] _directoryList;
	public MainWindow _fileSystemModel;

	/**
	 * 
	 *
	 * @author 
	 * @since 30.11.2011
	 *
	 * @param aParameter
	 */
	public void openDirectory(String aParameter) {
		throw new UnsupportedOperationException();
	}

	/**
	 * 
	 *
	 * @author 
	 * @since 30.11.2011
	 *
	 */
	private void filterMp3Files() {
		throw new UnsupportedOperationException();
	}

	/**
	 * 
	 *
	 * @author 
	 * @since 30.11.2011
	 *
	 * @param mp3SourceFile
	 * @return
	 * @throws FileSystemException
	 */
	public BufferedImage readId3Tag(File mp3SourceFile) throws FileSystemException{
		BufferedImage bi = null;
		
		MP3File mp3file;
		try {
			mp3file = new MP3File(mp3SourceFile);
		} catch (IOException e) {
			throw new FileSystemException("Konnte nicht \""+mp3SourceFile+"\" lesen");
		} catch (TagException e) {
			throw new FileSystemException("Fehler beim ID3 Tag lesen der Datei: \""+mp3SourceFile+"\"");
		}
		ID3v2_4 id3_23 = new ID3v2_4(mp3file.getID3v2Tag());
		
		@SuppressWarnings("rawtypes")
		Iterator it = id3_23.getFrameIterator();
		Object obj;
		ID3v2_4Frame frame;
		while (it.hasNext()) {
			obj = it.next();
			if (obj != null) {
				frame = (ID3v2_4Frame) obj;
				if(frame.getBody() instanceof FrameBodyAPIC){
					byte[] picByte = (byte[])(((FrameBodyAPIC)frame.getBody()).getObject("Picture Data"));
					if(picByte != null){
						ByteArrayInputStream bais = new ByteArrayInputStream(picByte);
						try{
							bi = ImageIO.read(bais);
							return bi;
						}catch(IOException ioe){
							throw new FileSystemException("Konnte nicht \""+mp3SourceFile+"\" ID3 Bild lesen");
						}
//						ImageIO.write(bi, "png", new File("C:\\img.png"));
					}
				}					
			}
		}
		
		throw new FileSystemException("Ende der readID3 Tag Methode Fehler");
	}

	/**
	 * 
	 *
	 * @author 
	 * @since 30.11.2011
	 *
	 * @param aActualPath
	 */
	public void setActualPath(String aActualPath) {
		this._actualPath = aActualPath;
	}

	/**
	 * 
	 *
	 * @author 
	 * @since 30.11.2011
	 *
	 * @return
	 */
	public String getActualPath() {
		return this._actualPath;
	}

	/**
	 * 
	 *
	 * @author 
	 * @since 30.11.2011
	 *
	 * @param aFileList
	 */
	public void setFileList(Track[] aFileList) {
		this._fileList = aFileList;
	}

	public Track[] getFileList() {
		return this._fileList;
	}

	public void setDirectoryList(String[] aDirectoryList) {
		this._directoryList = aDirectoryList;
	}

	public String[] getDirectoryList() {
		return this._directoryList;
	}
}